package anc.tankshooter.ui;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import anc.tankshooter.GameActivity;
import android.graphics.Color;

public class ResourcesManager
{
	public static enum typeSound {SHOOT, BOOM, EXPLODE,WIN,LOSE};
    public static enum typeMusic {MENU, GAME,MUTILGAME, FIGHTING};
    private static final ResourcesManager instance = new ResourcesManager();
    
    public Engine mEngine;
    public GameActivity mActivity;
    public Camera mCamera;
    public VertexBufferObjectManager mVertexManager;
    private Sound mShootSound, mBoomSound, mExplodeSound , mWinSound,mLoseSound;
    private Music mMenuMusic, mGameMusic, mFightingMusic,mMutilMusic;

    /**
     * Dùng để load sprite cho player
     */
    
    public ITextureRegion mLogoColorTexture;
    private BitmapTextureAtlas mLogoColorloader;
    
    private BitmapTextureAtlas mInvisibleEffectLoader;
    public ITiledTextureRegion mInvisibleEffectTexture;
    private BitmapTextureAtlas mITankTextureLoader;
    private BitmapTextureAtlas mBulletTextureLoader;
    private BitmapTextureAtlas mExplodeTextureLoader;
    private BitmapTextureAtlas mBumTextureLoader;
    private BitmapTextureAtlas mLaserExplodeLoader;
    
    public ITiledTextureRegion mTankTexture;
    public ITiledTextureRegion mBulletTexture;
    public ITiledTextureRegion mExplodeTexture;
    public ITiledTextureRegion mBumTexture;
    public ITiledTextureRegion mLaserExplodeTexture;
    
    private BitmapTextureAtlas mBackgroundLoader;
    public ITextureRegion mBackgroundTexture;
    
    public Font mWhiteFont20,mWhiteFont18,mWhiteFont15;
    private BitmapTextureAtlas mTextBoxLoader;
    public ITextureRegion mTextBoxTexture;
    
    private BitmapTextureAtlas mBtnLoader;
    public ITiledTextureRegion mBtnTexture;
    
    private BitmapTextureAtlas mDialogTitlesLoader;
    public ITiledTextureRegion mDialogTitlesTexture;
    
    private BitmapTextureAtlas mNumbersLoader;
    public ITiledTextureRegion mNumbersTexture;
    
    private BitmapTextureAtlas mMapPreviewLoader;
    public ITiledTextureRegion mMapPreviewTexture;
    
    //---------------------------------------------
    // MAN HINH MAIN MENU
    //---------------------------------------------
    private BitmapTextureAtlas mMainMenuTextureLoader;
    private BitmapTextureAtlas mMenuBackgroudTextureLoader;
    
    public ITextureRegion mMenuBackgroundTexture;
    public ITiledTextureRegion mMainMenuTexture;
    private BitmapTextureAtlas mPanelLoader;
    public ITextureRegion mPanelTexture;
    //---------------------------------------------
    // MAN HINH CHON LEVEL
    //---------------------------------------------
    
    private BitmapTextureAtlas mLevelBoxLoader;

    
    public ITiledTextureRegion mLevelBoxTexture;
    
    //---------------------------------------------
    // MAN HINH CHOI GAME : GAMESCREEN
    //---------------------------------------------
    private BitmapTextureAtlas mShootTextureLoader;
    private BitmapTextureAtlas mControllerTextureLoader, mKnobTextureLoader;
    
    public ITextureRegion mControllerTexture;
    public ITextureRegion mKnobTexture;
    public ITextureRegion mShootTexture;
    
    private BitmapTextureAtlas mHighScoreButtonLoader;
    public ITiledTextureRegion mHighScoreButtonTexture;
    
    private BitmapTextureAtlas mPauseButtonLoader;
    public ITiledTextureRegion mPauseButtonTexture;
    
    private BitmapTextureAtlas mSettingButtonLoader;
    public ITiledTextureRegion mSettingButtonTexture;
    
    private BitmapTextureAtlas mSettingBackgroundLoader;
    public ITextureRegion mSettingBackgoundTexture;
    
    private BitmapTextureAtlas mOkButtonLoader;
    private BitmapTextureAtlas mCancelButtonLoader;
    public ITiledTextureRegion mOkButton;
    public ITiledTextureRegion mCancelButton;
    private BitmapTextureAtlas mCheckBoxLoader;
    public ITiledTextureRegion mCheckBoxTexture;
    
    
    private BitmapTextureAtlas mResultPanelLoader;
    public ITextureRegion mResultPanelTexture;
    
   //---------------------------------------------
   // MAN HINH LOGIN SERVER : 
   //---------------------------------------------
    private BitmapTextureAtlas mLoginBackgroundLoader;
    public ITextureRegion mLoginBackgroundTexture;
    
    
    
    
    
    //---------------------------------------------
    // CLASS LOGIC
    //---------------------------------------------
    
    public void loadGlobalResources() {

    	this.mITankTextureLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 256, 320,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	mTankTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mITankTextureLoader,mActivity,"images/player/tanks.png", 0, 0, 8,10);
    	this.mITankTextureLoader.load();
    	
    	this.mBulletTextureLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 128,64,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	this.mBulletTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBulletTextureLoader,mActivity,"images/weapon/smallbullet.png", 0, 0, 4, 2);
    	this.mBulletTextureLoader.load();
    	
    	this.mExplodeTextureLoader = new BitmapTextureAtlas(mActivity.getTextureManager(),320, 320,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	this.mExplodeTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mExplodeTextureLoader, mActivity, "images/weapon/explosion.png", 0, 0, 5, 5);
    	this.mExplodeTextureLoader.load();
    	
    	this.mBumTextureLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	this.mBumTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBumTextureLoader, mActivity, "images/weapon/bum.png", 0 , 0, 4, 4);
    	this.mBumTextureLoader.load();
    	
    	this.mLaserExplodeLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 400, 400, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	this.mLaserExplodeTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mLaserExplodeLoader, mActivity, "images/weapon/laser.png", 0 , 0, 5, 5);
    	this.mLaserExplodeLoader.load();
    	
    	this.mLogoColorloader = new BitmapTextureAtlas(mActivity.getTextureManager(), 390, 100,TextureOptions.BILINEAR);
    	this.mLogoColorTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mLogoColorloader, mActivity, "images/logo_color.png", 0, 0);
    	this.mLogoColorloader.load();
    	
    	this.mBackgroundLoader=new BitmapTextureAtlas(mActivity.getTextureManager(), 800, 480,TextureOptions.BILINEAR);
    	this.mBackgroundTexture=BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBackgroundLoader, mActivity, "images/splashcreen.jpg", 0, 0);
    	this.mBackgroundLoader.load();
    	
    	mWhiteFont20 = FontFactory.createFromAsset(mActivity.getFontManager(), mActivity.getTextureManager(), 128, 128,
    						mActivity.getAssets(),"fonts/btr.ttf", 20, true,Color.WHITE);
    	mWhiteFont20.load();
    	
    	mWhiteFont18 = FontFactory.createFromAsset(mActivity.getFontManager(), mActivity.getTextureManager(), 128, 128,
				mActivity.getAssets(),"fonts/btr.ttf", 18, true,Color.WHITE);
    	
    	mWhiteFont18.load();
    	
    	mWhiteFont15 = FontFactory.createFromAsset(mActivity.getFontManager(), mActivity.getTextureManager(), 128, 128,
				mActivity.getAssets(),"fonts/btr.ttf",15, true,Color.WHITE);
    	
    	mWhiteFont15.load();
    	
    	this.mTextBoxLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 300,45,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	this.mTextBoxTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mTextBoxLoader, mActivity, "images/textbox_background.png",0,0);
    	this.mTextBoxLoader.load();

    	this.mBtnLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 320,240,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	this.mBtnTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBtnLoader, mActivity, "images/btn.png",0,0,4,8);
    	this.mBtnLoader.load();
    	
    	mDialogTitlesLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 390,308, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mDialogTitlesTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mDialogTitlesLoader, mActivity, "images/dialog.titles.png", 0, 0,1,7);
        mDialogTitlesLoader.load();
    	
        mNumbersLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 605,55, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mNumbersTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mNumbersLoader, mActivity, "images/numbers.png", 0, 0,11, 1);
        mNumbersLoader.load();
        
        mPanelLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 386, 250, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	mPanelTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mPanelLoader, mActivity, "images/box_panel.png", 0,0);
    	mPanelLoader.load();
       
    	mInvisibleEffectLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 192, 192, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	mInvisibleEffectTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mInvisibleEffectLoader, mActivity, "images/blind.png", 0,0,3,3);
     	mInvisibleEffectLoader.load();
     	
     	mMapPreviewLoader = new BitmapTextureAtlas(mActivity.getTextureManager(),400, 750, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	mMapPreviewTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mMapPreviewLoader, mActivity, "images/maps.png", 0,0,1,3);
    	mMapPreviewLoader.load();
     	
    	this.mLoginBackgroundLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 310,293,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	this.mLoginBackgroundTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mLoginBackgroundLoader, mActivity, "images/loginscreen.png",0,0);
    	this.mLoginBackgroundLoader.load();
    	
    	loadSoundAndMusicResource();
    }
    private void loadSoundAndMusicResource()
    {
    	try {
			mShootSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), mActivity, "sound/shoot1.wav");
			mShootSound.setVolume(0.2f);
			
			mBoomSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), mActivity, "sound/BoomSound.mp3");
			mBoomSound.setVolume(0.5f);
			
			mExplodeSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), mActivity, "sound/ExplodeSound.mp3");
			mExplodeSound.setVolume(1f);
			
			mWinSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), mActivity, "sound/Win.mp3");
			mWinSound.setVolume(1f);
			
			mLoseSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), mActivity, "sound/Lose.mp3");
			mLoseSound.setVolume(1f);
			
			mMenuMusic = MusicFactory.createMusicFromAsset(mEngine.getMusicManager(), mActivity, "sound/MenuMusic.mp3");
			mGameMusic = MusicFactory.createMusicFromAsset(mEngine.getMusicManager(), mActivity, "sound/GameMusic.mp3");
			mGameMusic.setLooping(true);
			mGameMusic.setVolume(0.3f);
			
			mFightingMusic = MusicFactory.createMusicFromAsset(mEngine.getMusicManager(), mActivity, "sound/FightingClose.mp3");
			mFightingMusic.setLooping(true);
			mFightingMusic.setVolume(0.25f);
			
			mMutilMusic = MusicFactory.createMusicFromAsset(mEngine.getMusicManager(), mActivity, "sound/MultiGame.mp3");
			mMutilMusic.setLooping(true);
			mMutilMusic.setVolume(0.25f);
			
		} catch (Exception e) {
		}
    }
    
    public void loadSplashScreen() {
    }
    public void unloadSplashScreen() {
    }
    
    public void loadLevelSelectionResources() {
    	
    	mLevelBoxLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 96,166,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	mLevelBoxTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mLevelBoxLoader, mActivity, "images/button_level.png", 0, 0,1, 2);
    	mLevelBoxLoader.load();
    }
    public void unloadLevelSelectionResources() {
 
    	mPanelLoader.unload();
    	mLevelBoxLoader.unload();
    }
    
    public void loadMenuResources() {
    	
    	playMusic(typeMusic.MENU);
    	mMenuBackgroudTextureLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 800, 480, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	mMenuBackgroundTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuBackgroudTextureLoader, mActivity, "images/splashcreen.jpg", 0,0);
    	mMenuBackgroudTextureLoader.load();
    	
    	mMainMenuTextureLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 500, 195, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	mMainMenuTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mMainMenuTextureLoader, mActivity, "images/mainmenu.png", 0, 0, 2, 5);
    	mMainMenuTextureLoader.load();
    }
    public void unloadMenuResources() {
    	pauseMusic(typeMusic.MENU);
    	mMainMenuTextureLoader.unload();
    	mMenuBackgroudTextureLoader.unload();
    }
    
    public void loadGameResources() {
    	
        mControllerTextureLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 128, 128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mControllerTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mControllerTextureLoader, mActivity,"images/controller/base.png",0,0);
        mControllerTextureLoader.load();
        
        mKnobTextureLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 64, 64,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mKnobTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mKnobTextureLoader, mActivity,"images/controller/knob.png",0,0);
        mKnobTextureLoader.load();
        
        mShootTextureLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 64, 64,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mShootTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mShootTextureLoader, mActivity,"images/controller/knob.png",0,0);
        mShootTextureLoader.load();       
        
        mHighScoreButtonLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 100, 50, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mHighScoreButtonTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mHighScoreButtonLoader, mActivity, "images/controller/btn_highscore.png", 0, 0, 2, 1);
        mHighScoreButtonLoader.load();
        
        mPauseButtonLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 100, 100, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mPauseButtonTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mPauseButtonLoader, mActivity, "images/controller/PlayPauseButton.png", 0, 0, 2, 2);
        mPauseButtonLoader.load();
        
        mSettingButtonLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 100, 50, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mSettingButtonTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mSettingButtonLoader, mActivity, "images/controller/Setting.png", 0, 0, 2, 1);
        mSettingButtonLoader.load();
        
        mSettingBackgroundLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 386, 250, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mSettingBackgoundTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mSettingBackgroundLoader, mActivity, "images/SettingPanel.png", 0, 0);
        mSettingBackgroundLoader.load();
        
        mOkButtonLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 300, 50, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mCancelButtonLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 300, 50, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mOkButton = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mOkButtonLoader, mActivity, "images/controller/btn_ok.png", 0, 0, 2, 1);
        mCancelButton = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mCancelButtonLoader, mActivity, "images/controller/btn_cancle.png", 0, 0, 2, 1);
        mOkButtonLoader.load();
        mCancelButtonLoader.load();
        
        mCheckBoxLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 128, 25, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mCheckBoxTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mCheckBoxLoader, mActivity, "images/controller/Checkbox.png", 0, 0, 2, 1);
        mCheckBoxLoader.load();
        
        mResultPanelLoader = new BitmapTextureAtlas(mActivity.getTextureManager(), 390, 300, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mResultPanelTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mResultPanelLoader, mActivity, "images/resultpabnel.png", 0, 0);
        mResultPanelLoader.load();
    }
    public void unloadGameResources() {
    	pauseMusic(typeMusic.GAME);
    	pauseMusic(typeMusic.FIGHTING);
    	pauseMusic(typeMusic.MUTILGAME);
        mControllerTextureLoader.unload();
        mKnobTextureLoader.unload();
        mShootTextureLoader.unload();
    }
    
    public static void prepareManager(Engine engine, GameActivity activity, Camera camera, VertexBufferObjectManager vbom)
    {
        getInstance().mEngine = engine;
        getInstance().mActivity = activity;
        getInstance().mCamera = camera;
        getInstance().mVertexManager = vbom;
    }
    public static ResourcesManager getInstance()
    {
        return instance;
    }
    
    public void playSound(typeSound type)
    {
    	if(GameActivity.mGlobalSetting.isSoundOn())
    	{
    		switch (type) {
			case BOOM:
				mBoomSound.play();
				break;
			case SHOOT:
				mShootSound.play();
				break;
			case EXPLODE:
				mExplodeSound.play();
				break;
			case WIN:
				mWinSound.play();
				break;
			case LOSE:
				mLoseSound.play();
				break;
			default:
				break;
			}
    	}
    }
    
    public void playMusic(typeMusic type)
    {
    	if(GameActivity.mGlobalSetting.isMusicOn()) {
	    	switch (type) {
			case MENU:
				mMenuMusic.play();
				break;
			case GAME:
				mGameMusic.play();
				break;
			case MUTILGAME:
				mMutilMusic.play();
				break;
			case FIGHTING:
				mFightingMusic.play();
			default:
				break;
			}
    	}
    }
    public void pauseMusic(typeMusic type)
    {
    	switch (type) {
		case MENU:
			mMenuMusic.pause();
			break;
		case GAME:
			mGameMusic.pause();
			break;
		case MUTILGAME:
			mMutilMusic.pause();
			break;
		case FIGHTING:
			mFightingMusic.pause();
		default:
			break;
		}
    }
    public void pauseMusic()
    {
		mMenuMusic.pause();
		mGameMusic.pause();
		mFightingMusic.pause();
		mMutilMusic.pause();
    }
    public void resumeMusic()
    {
    	mMenuMusic.play();
		mGameMusic.play();
		mFightingMusic.play();
		
    }
}












